package config.xml;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class file {

	/**
	 * @param args
	 * @throws IOException
	 */
	private static String user;
	private static String password;
	private static String DBname;

	public static String getDBname() {
		return DBname;
	}

	public void setDBname(String dBname) {
		this.DBname = dBname;
	}

	public static String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public static String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void set() throws IOException {// 生成mybatis-generator所需的配置文件
		BufferedReader in = null;
		BufferedWriter out = null;

		try {
			out = new BufferedWriter(new FileWriter("./output/generator.xml"));
			in = new BufferedReader(new FileReader(
					"./generator/generatorConfig.xml"));
			String i = null;
			int j = 0;
			while ((i = in.readLine()) != null) {
				if (j == 13) {
					out.write("    "
							+ "connectionURL=\"jdbc:mysql://localhost:3306/"
							+ this.DBname + "\"\r\n");
				} else if (j == 14) {
					out.write("    " + "userId=\"" + this.user
							+ "\" password=\"" + this.password + "\" />"
							+ "\r\n");
					// System.out.println( "    "+"userId=\""+ this.user
					// +"\" password=\""+this.password+"\" />"+"\r\n" );
				} else if (j == 22) {
					for (String tableName : DBuse.getTableNames()) {
						out.write("    " + "<table tableName=\"" + tableName
								+ "\" />" + "\r\n");
					}
				} else
					out.write(i + "\r\n");
				j++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			in.close();
			out.close();
		}
	}

	public void cmd() { // 调用cmd命令行执行mybatis-generator配置文件
		try {
			String u = "java -jar .\\lib\\mybatis-generator-core-1.3.1.jar -configfile .\\output\\generator.xml -overwrite";
			Runtime.getRuntime().exec("cmd /c start " + u);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
